﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace PRT
{
    public class DataboundLabel : DataBoundControl
    {
        #region Properties
        [Themeable(false), Description("The field in the data source that provides the text."), DefaultValue(""), Category("Data")]
        public virtual string DataTextField
        {
            get
            {
                object o = this.ViewState["DataTextField"];
                if (o != null)
                {
                    return (string)o;
                }
                return string.Empty;
            }
            set
            {
                this.ViewState["DataTextField"] = value;

                base.OnDataPropertyChanged();
            }
        }


        [DefaultValue(""), Themeable(false), Description("The formatting applied to the text. For example, {0:d}."), Category("Data")]
        public virtual string DataTextFormatString
        {
            get
            {
                object o = this.ViewState["DataTextFormatString"];
                if (o != null)
                {
                    return (string)o;
                }
                return string.Empty;
            }
            set
            {
                this.ViewState["DataTextFormatString"] = value;

                base.OnDataPropertyChanged();
            }
        }

        [Category("Accessibility"), IDReferenceProperty, Description("The ID of the control associated with the Label."), Themeable(false), TypeConverter(typeof(AssociatedControlConverter)), DefaultValue("")]
        public virtual string AssociatedControlID
        {
            get
            {
                string str = (string)this.ViewState["AssociatedControlID"];
                if (str != null)
                {
                    return str;
                }
                return string.Empty;
            }
            set
            {
                this.ViewState["AssociatedControlID"] = value;
            }
        }

        protected override HtmlTextWriterTag TagKey
        {
            get
            {
                if (this.AssociatedControlID.Length != 0)
                {
                    return HtmlTextWriterTag.Label;
                }
                return base.TagKey;
            }
        }

        public virtual string Text
        {
            get
            {
                object o = this.ViewState["Text"];

                if (o != null)
                    return (string)o;
                else
                    return string.Empty;
            }
        }
        #endregion

        #region Methods
        protected override void PerformDataBinding(System.Collections.IEnumerable data)
        {
            base.PerformDataBinding(data);

            if (data != null)
            {
                // Clear out the Text property
                this.ClearText();

                string formatString = "{0}";
                if (string.IsNullOrEmpty(this.DataTextFormatString) == false)
                    formatString = this.DataTextFormatString;

                // Get the DataTextFormatString field value for the FIRST record
                foreach (object obj in data)
                {
                    if (base.DesignMode)
                        SetText(string.Format(formatString, "abc"));
                    else
                    {
                        if (string.IsNullOrEmpty(this.DataTextField))
                            SetText(string.Format(formatString, obj.ToString()));
                        else
                            SetText(DataBinder.GetPropertyValue(obj, this.DataTextField, formatString));
                    }

                    break;
                }
            }
        }

        protected override void AddAttributesToRender(HtmlTextWriter writer)
        {
            if (!string.IsNullOrEmpty(this.AssociatedControlID))
            {
                Control control = this.FindControl(this.AssociatedControlID);
                if (control == null && !base.DesignMode)
                    throw new HttpException(string.Format("The DataboundLabel '{0}' cannot find associated control ID '{1}'.", this.ID, this.AssociatedControlID));
                else
                    writer.AddAttribute(HtmlTextWriterAttribute.For, control.ClientID);
            }

            base.AddAttributesToRender(writer);
        }

        protected override void RenderContents(HtmlTextWriter writer)
        {
            writer.Write(this.Text);
        }


        protected virtual void SetText(string txt)
        {
            this.ViewState["Text"] = txt;
        }

        protected virtual void ClearText()
        {
            this.ViewState.Remove("Text");
        }
        #endregion
    }
}